import Vue from 'vue'
import VueRouter from 'vue-router'
import HasNavigation from "../layout/HasNavigation"
import store from "../store"
import { Notify } from "vant"

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    meta: {
      auth: false
    },
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/',
    redirect: '/selectProductionLine',
    component: HasNavigation,
    children: [
      {
        path: '/home',
        name: 'Home',
        component: () => import('../views/Home.vue'),
      },
      {
        path: '/ldhome',
        name: 'LDHome',
        component: () => import('../views/Homes/LDHome.vue'),
      },
      {
        path: '/mm3home',
        name: 'MM3Home',
        component: () => import('../views/Homes/MM3Home.vue'),
      },
      {
        path: '/m1home',
        name: 'M1Home',
        component: () => import('../views/Homes/M1Home.vue'),
      },
      {
        path: '/m2home',
        name: 'M2Home',
        component: () => import('../views/Homes/M2Home.vue'),
      },
      {
        path: '/ccohome',
        name: 'CCOHome',
        component: () => import('../views/Homes/CCOHome.vue'),
      },
      {
        path: '/conhome',
        name: 'CONHome',
        component: () => import('../views/Homes/CONHome.vue'),
      },
      {
        path: '/m1m2packhome',
        name: 'M1M2PackHome',
        component: () => import('../views/Homes/M1M2PackHome.vue'),
      },
      {
        path: '/mmpshome',
        name: 'MMPSHome',
        component: () => import('../views/Homes/MMPSHome.vue'),
      },
      {
        path: '/whitehome',
        name: 'whiteHome',
        component: () => import('../views/Homes/WhiteHome.vue'),
      },
      {
        path: '/dovehome',
        name: 'dovehome',
        component: () => import('../views/Homes/DoveHome.vue'),
      },
      {
        path: '/dovelighthome',
        name: 'dovelighthome',
        component: () => import('../views/Homes/DoveLightHome.vue'),
      },
      {
        path: '/dovemakehome',
        name: 'dovemakehome',
        component: () => import('../views/Homes/DoveMakeHome.vue'),
      },
      {
        path: '/doveputhome',
        name: 'doveputhome',
        component: () => import('../views/Homes/DovePutHome.vue'),
      },
      {
        path: '/mdhome',
        name: 'mdhome',
        component: () => import('../views/Homes/MDHome.vue'),
      },
      {
        path: '/md4home',
        name: 'md4home',
        component: () => import('../views/Homes/MD4Home.vue'),
      },
      {
        path: '/receiptConfirmation',
        meta: {
          title: '收货确认'
        },
        component: () => import('../views/material/ReceiptConfirmation.vue')
      },
      {
        path: '/return',
        meta: {
          title: '产线退货'
        },
        component: () => import('../views/material/Return.vue')
      },
      {
        path: '/stateAdjustment',
        meta: {
          title: '状态调整'
        },
        component: () => import('../views/material/StateAdjustment.vue')
      },
      {
        path: '/quantityAdjustment',
        meta: {
          title: '数量调整'
        },
        component: () => import('../views/material/QuantityAdjustment.vue')
      },
    ]
  },
  {
    path: '/feeding',
    component: HasNavigation,
    children: [
      {
        path: 'wip',
        name: 'WIP',
        meta: {
          title: '直接投料'
        },
        component: () => import('../views/feeding/WIP')
      },
      {
        path: 'device',
        name: 'Device',
        meta: {
          title: '投料口投料'
        },
        component: () => import('../views/feeding/Device')
      },
      {
        path: 'standardQuantity',
        name: 'StandardQuantity',
        meta: {
          title: '标准量投料'
        },
        component: () => import('../views/feeding/StandardQuantity')
      },
      {
        path: 'formulaRatio',
        name: 'FormulaRatio',
        meta: {
          title: '配方比投料',
          /* keepAlive: true */
        },
        component: () => import('../views/feeding/FormulaRatio')
      },
      {
        path: 'workOrder',
        name: 'WorkOrder',
        meta: {
          title: '工单投料'
        },
        component: () => import('../views/feeding/WorkOrder')
      },
      {
        path: 'MindWorkOrder',
        name: 'MindWorkOrder',
        meta: {
          title: 'PAN工单投料'
        },
        component: () => import('../views/feeding/MindWorkOrder')
      },
      {
        path: 'blending',
        name: 'Blending',
        meta: {
          title: 'Blending投料'
        },
        component: () => import('../views/feeding/Blending')
      },
      {
        path: 'deRisky',
        name: 'DeRisky',
        meta: {
          title: 'De-risky投料'
        },
        component: () => import('../views/feeding/DeRisky')
      },
      {
        path: 'membraneRoll',
        name: 'MembraneRoll',
        meta: {
          title: '膜卷投料'
        },
        component: () => import('../views/feeding/MembraneRoll')
      },
      {
        path: 'rollreceipt',
        name: 'rollreceipt',
        meta: {
          title: '膜卷领料'
        },
        component: () => import('../views/feeding/RollReceipt')
      },
      {
        path: 'back',
        name: 'Back',
        meta: {
          title: '投料退回'
        },
        component: () => import('../views/feeding/Back.vue')
      },
      {
        path: 'searchdate',
        name: 'Searchdate',
        meta: {
          title: '查询信息'
        },
        component: () => import('../views/feeding/SearchDate.vue')
      },
      {
        path: 'workagain',
        name: 'WorkAgain',
        meta: {
          title: '返工投料'
        },
        component: () => import('../views/feeding/WorkAgain.vue')
      },
      {
        path: 'returnaccept',
        name: 'ReturnAccept',
        meta: {
          title: '返工收货'
        },
        component: () => import('../views/goback/ReturnAccept.vue')
      },
    ]
  },
  {
    path: '/selectProductionLine',
    name: 'SelectProductionLine',
    component: () => import('../views/SelectProductionLine.vue')
  },
]

const router = new VueRouter({
  // mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (store.getters.user) {
    if (to.name !== 'Login') {
      next()
    } else {
      Notify({ type: 'primary', message: '您已登录，无法跳转到登录页面' });
      next(from.path)
    }
  } else {
    if (to.name === 'Login') {
      next()
    }
    store.dispatch('loadUser').then(() => {
      next(from.path)
    }).catch(() => {
      next('/login')
    })
  }
})

export default router
